package cn.com.winning.ecare.activity;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import cn.com.winning.ecare.common.MyApplication;
import cn.com.winning.ecare.utils.HTTPGetTool;
import cn.com.winning.ecare.utils.URLUtils;

/**找回密码
 * @author Administrator
 *
 */
public class Findpassword extends Activity
{
	private Findpassword oThis = this;
	private EditText username, realname, pcard, phoneno;
	private Button saveButton;
	private ImageButton cancelButton;
	private ProgressDialog proDialog;
	private boolean flag = true;
	InputMethodManager manager;
	MyApplication myApplication;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.findpassword);
		myApplication = (MyApplication) getApplication();
		myApplication.addActivity(this);
		manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		// 得到相应的显示控件的对象
		username = (EditText) findViewById(R.id.username);
//		realname = (EditText) findViewById(R.id.realname);
//		pcard = (EditText) findViewById(R.id.pcard);
		phoneno = (EditText) findViewById(R.id.phoneno);
		
		username.setOnFocusChangeListener(this.onFocusAutoClearHintListener);
//		realname.setOnFocusChangeListener(this.onFocusAutoClearHintListener);
//		pcard.setOnFocusChangeListener(this.onFocusAutoClearHintListener);
		phoneno.setOnFocusChangeListener(this.onFocusAutoClearHintListener);
		
		saveButton = (Button) findViewById(R.id.save_button);
		cancelButton = (ImageButton) findViewById(R.id.imageback);
		cancelButton.setClickable(true);

		// 为saveButton 注册一个点击事件监听器
		saveButton.setOnClickListener(new View.OnClickListener()
		{
			// 当按钮被点击时调用该方法
			public void onClick(View v)
			{
				findPasswd();
			}
		});

		cancelButton.setOnClickListener(new View.OnClickListener()
		{
			// 当按钮被点击时调用该方法
			public void onClick(View v)
			{
				oThis.onBackPressed();
			}
		});
	}

	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		// TODO Auto-generated method stub
		if (event.getAction() == MotionEvent.ACTION_DOWN)
		{
			if (getCurrentFocus() != null && getCurrentFocus().getWindowToken() != null)
			{
				manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
			}
		}
		return super.onTouchEvent(event);
	}
	
	public static OnFocusChangeListener onFocusAutoClearHintListener = new OnFocusChangeListener() {
		@Override
		public void onFocusChange(View v, boolean hasFocus) {
			EditText textView = (EditText) v;
			String hint;
			if (hasFocus) {
				hint = textView.getHint().toString();
				textView.setTag(hint);
				textView.setHint("");
			} else {
				hint = textView.getTag().toString();
				textView.setHint(hint);
			}
		}
	};

	/**
	 * 拼写检测，检测输入内容是否合乎要求
	 */
	public void findPasswd()
	{
		if (username.getText().toString().length() == 0)
		{
			username.setError("用户名不能为空");
		}
//		else if (realname.getText().toString().length() == 0)
//		{
//			realname.setError("真实姓名不能为空");
//		}
//		else if (pcard.getText().toString().length() == 0)
//		{
//			pcard.setError("身份证号不能为空");
//		}
		else if (phoneno.getText().toString().length() == 0)
		{
			phoneno.setError("手机号不能为空");
		}
		else
		{
			proDialog = ProgressDialog.show(Findpassword.this, "", "查找中...", true, true);

			String passwdInfo = checkInfo();
			if (!passwdInfo.equals(""))
			{
				Toast.makeText(Findpassword.this, "验证成功，密码:" + passwdInfo, Toast.LENGTH_LONG).show();
				// 到下一页面
				Intent intent = new Intent();
				intent.setClass(Findpassword.this, Login.class);
				startActivity(intent);
			}
			else
			{
				Toast.makeText(Findpassword.this, "验证失败，验证信息与注册信息不符", Toast.LENGTH_SHORT).show();
			}
			proDialog.dismiss();
		}
	}

	/**
	 * 验证注册信息
	 */
	public String checkInfo()
	{
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("username", username.getText().toString().trim()));
//		params.add(new BasicNameValuePair("realname", realname.getText().toString().trim()));
//		params.add(new BasicNameValuePair("pcard", pcard.getText().toString().trim()));
		params.add(new BasicNameValuePair("phoneno", phoneno.getText().toString().trim()));
		JSONObject res = HTTPGetTool.getTool().post(URLUtils.HOSTMAIN + URLUtils.FINDPW, params);
		String passwdInfo = "";
		if (res != null)
		{
			try
			{
				passwdInfo = res.get("passwdInfo").toString();
			}
			catch (JSONException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return passwdInfo;
	}
}